package com.hiof.overlays;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.hiof.R;
import com.hiof.location.LocationChangedListener;
import com.hiof.servercommunication.receive.beans.GeoPoint;
import com.hiof.settings.user.TypeOfAttendant;
import com.hiof.settings.user.UserConfiguration;

/**
 * Created with IntelliJ IDEA.
 * User: manish
 * Date: 4/17/12
 * Time: 8:04 PM
 * To change this template use File | Settings | File Templates.
 */
public class OwnOverlay extends Overlay implements LocationChangedListener {
    private GeoPoint geoPoint;
    private Resources resources;
    private int markerID = R.drawable.cycling;

//    private MapView mapView;

    public OwnOverlay(Resources resources, GeoPoint geoPoint) {
        this.resources = resources;
        this.geoPoint = geoPoint;
        setupMarker();
    }

    private void setupMarker() {
        if(UserConfiguration.getInstance().getTypeOfAttendant() == TypeOfAttendant.SUPPORT){
            markerID = R.drawable.supportselfmarker;
        }
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean b) {
        super.draw(canvas, mapView, false);
        drawOwnLocation(canvas, mapView);
    }

    private void drawOwnLocation(Canvas canvas, MapView mapView) {
        Point screenPt = new Point();
        mapView.getProjection().toPixels(geoPoint.getGoogleGeoPoint(), screenPt);
        drawMarker(canvas, markerID, resources, screenPt.x, screenPt.y);
    }

    public static void drawMarker(Canvas canvas, int id, Resources resources,
                                  int x, int y) {
        Bitmap bitmap = BitmapFactory.decodeResource(resources, id);
        canvas.drawBitmap(bitmap, x - 10, y - 10, null);
    }

    @Override
    public void updateLocationChanged(GeoPoint geoPoint) {
        this.geoPoint = geoPoint;
    }
}
